package com.feel.modules.auth.controller;

import cn.dev33.satoken.oauth2.logic.SaOAuth2Handle;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
import cn.dev33.satoken.oauth2.model.AccessTokenModel;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feel.common.constant.HttpStatus;
import com.feel.common.exception.AuthException;
import com.feel.common.utils.R;
import com.feel.modules.auth.entity.OauthClientDetailsEntity;
import com.feel.modules.auth.service.OauthClientDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@Api("Token测试")
public class TokenController implements Serializable {
    //根据code获取token
    @Autowired
    private OauthClientDetailsService saClientService;

    // 处理所有OAuth相关请求
    @GetMapping("/oauth2/token")
    public R<Object> request() {
        return R.ok(SaOAuth2Handle.serverRequest());
    }


    @GetMapping("/oauth2/getTokenByCode")
    @ApiOperation(value = "根据code获取token", notes = "根据code获取token")
    public R<Object> getTokenByCode(@ApiParam(value = "clientId" , required=true ) @RequestParam String clientId,
                                         @ApiParam(value = "code" , required=true ) @RequestParam String code,
                                         @ApiParam(value = "redirectUri" , required=false ) @RequestParam(required=false) String redirectUri) {
        QueryWrapper<OauthClientDetailsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("client_id",clientId);
        OauthClientDetailsEntity oauthclient = saClientService.getOne(wrapper);
        if(oauthclient ==null ){
            throw new AuthException(new R(HttpStatus.FORBIDDEN));
        }
        // 校验参数
        SaOAuth2Util.checkGainTokenParam(code, clientId, oauthclient.getClientSecret(), redirectUri);
        // 构建 Access-Token
        AccessTokenModel token = SaOAuth2Util.generateAccessToken(code);
        //获取token
        return  R.ok(token);
    }

    @GetMapping("/oauth2/refreshToken")
    @ApiOperation(value = "根据code获取token", notes = "根据code获取token")
    public R<Object> refreshToken(@ApiParam(value = "clientId" , required=true ) @RequestParam String clientId,
                                             @ApiParam(value = "refreshToken" , required=true ) @RequestParam String refreshToken) {
        QueryWrapper<OauthClientDetailsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("client_id",clientId);
        OauthClientDetailsEntity oauthclient = saClientService.getOne(wrapper);
        if(oauthclient ==null ){
            throw new AuthException(new R(HttpStatus.FORBIDDEN));
        }
        // 校验参数
        SaOAuth2Util.checkRefreshTokenParam(clientId, oauthclient.getClientSecret(), refreshToken);
        // 获取新Token返回
        Object data = SaOAuth2Util.refreshAccessToken(refreshToken).toLineMap();

        return  R.ok(data);
    }

    //退出登陆

    @GetMapping("/oauth2/logout")
    public R<Object> logout() {
        StpUtil.logout();
        return R.ok("success");
    }



}
